class Run < ActiveRecord::Base
  # TODO: 100% трябва да има по-кратък и лесен начин да се направи това.
  validates_presence_of :problem_id
  validates_presence_of :user_id
  validates_presence_of :submit_time
  validates_presence_of :language
  validates_presence_of :source_code
  validates_presence_of :source_name
  validates_presence_of :about
  validates_presence_of :status
  validates_presence_of :log
  validates_numericality_of :problem_id,:user_id
  validates_presence_of :problem
  validates_presence_of :potrebitel,:message => "should be valid"
  validate :language_check
  belongs_to :problem
  belongs_to :potrebitel,:class_name => "User",:foreign_key => "user_id"
  self.primary_key = "run_id"
  include Configurator 

  cattr_reader :per_page
  @@per_page = 100

  def user
    User.find_by_id(self.user_id)
  end
  
  def user_name 
    self.user.login
  end

  def problem_name
    self.problem.name
  end

  def contest_name
    self.problem.contest.name
  end

  def language_check
    config = load_config
    unless config['language'].values.include?(self.language)
      self.errors.add("language","is not valid")
      false
    else
      true
    end
  end
end
